package com.example;

import java.sql.SQLException;
import java.util.List;

public class DataInitializer {

    public static void addSampleStudents(UserDAO userDAO) {
        String studentData = ""
            + "202308418 阿的克古 student 计算机科学与技术 active 123456\n"
            + "202308398 聂宇豪 student 计算机科学与技术 active 123456\n"
            + "202305483 郑耀东 student 电气 active 123456\n"
            + "202308404 周镓俊 student 计算机科学与技术 active 123456\n"
            + "202308419 张宜萍 student 计算机科学与技术 active 123456\n"
            + "202405698 郑嘉铭 student 电气 active 123456\n"
            + "202308315 禹虹阳 student 计算机科学与技术 active 123456\n"
            + "202308393 杨雨昊 student 计算机科学与技术 active 123456\n"
            + "202308396 张国超 student 计算机科学与技术 active 123456\n"
            + "202308399 余乐 student 计算机科学与技术 active 123456\n"
            + "202308316 杨灿 student 计算机科学与技术 active 123456\n"
            + "202308400 杨嘉宁 student 计算机科学与技术 active 123456\n"
            + "202308405 许又尹 student 计算机科学与技术 active 123456\n"
            + "202308408 肖坤孟 student 计算机科学与技术 active 123456\n"
            + "202308409 文国权 student 计算机科学与技术 active 123456\n"
            + "202308421 严漫漫 student 计算机科学与技术 active 123456\n"
            + "202308394 王嘉浩 student 计算机科学与技术 active 123456\n"
            + "202308395 王博成 student 计算机科学与技术 active 123456\n"
            + "202308403 王启 student 计算机科学与技术 active 123456\n"
            + "202308407 王胤丞 student 计算机科学与技术 active 123456\n"
            + "202308412 王鑫国 student 计算机科学与技术 active 123456\n"
            + "202308413 王亦名 student 计算机科学与技术 active 123456\n"
            + "202308417 陶军红 student 计算机科学与技术 active 123456\n"
            + "202308420 王欣怡 student 计算机科学与技术 active 123456\n"
            + "202308427 王彤 student 计算机科学与技术 active 123456\n"
            + "202405699 王逸天 student 电气 active 123456"
            // Added more sample student data below
            + "\n202308501 李明 student 软件工程1班 active 123456"
            + "\n202308502 张华 student 软件工程1班 active 123456"
            + "\n202308503 王芳 student 计算机科学2班 active 123456"
            + "\n202308504 赵强 student 计算机科学2班 active 123456"
            + "\n202308505 刘伟 student 信息管理3班 active 123456"
            + "\n202308506 杨丽 student 信息管理3班 active 123456"
            + "\n202308507 陈刚 student 软件工程1班 active 123456"
            + "\n202308508 吴静 student 计算机科学2班 active 123456"
            + "\n202308509 冯波 student 信息管理3班 active 123456"
            + "\n202308510 郭梅 student 电气 active 123456"
            + "\n202308511 孙涛 student 电气 active 123456"
            + "\n202308512 胡丽 student 未知班级 active 123456"
            + "\n202308513 朱军 student 未知班级 active 123456";

        String[] lines = studentData.split("\\n");

        try {
             System.out.println("Checking and adding sample student data...");
            for (String line : lines) {
                String[] parts = line.trim().split("\\s+");
                 if (parts.length >= 6) { // Ensure there are enough parts for ID, name, type, department, status, password
                    String studentId = parts[0];

                    // Check if student already exists
                    if (userDAO.getUserByUsername(studentId) == null) {
                         String studentName = parts[1];
                         String type = parts[2]; // Should be "student"
                         StringBuilder departmentBuilder = new StringBuilder();
                         // Assuming department is the 4th part (index 3) and could have spaces
                         for(int i = 3; i < parts.length - 2; i++) { // Capture multi-word department names, excluding last 2 parts (status, password)
                              departmentBuilder.append(parts[i]).append(" ");
                         }
                         String department = departmentBuilder.toString().trim();
                         String status = parts[parts.length - 2]; // Assuming status is second to last
                         String password = parts[parts.length - 1]; // Assuming password is last

                         User newUser = new User(studentId, studentName, type, department, status, password);
                         userDAO.addUser(newUser);
                         System.out.println("Added new student: " + studentId);
                    } else {
                         System.out.println("Student already exists, skipping: " + studentId);
                    }

                } else {
                    System.err.println("Skipping invalid data line during insertion: " + line);
                }
            }
            System.out.println("Finished checking and adding sample student data.");

        } catch (SQLException e) {
            e.printStackTrace();
            System.err.println("Database error during sample student data insertion.");
        }
    }

} 